package com.boco.pciv.controller.sys;

import cn.hutool.json.JSONUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.core.exception.PcivException;
import com.boco.pciv.core.security.LoginUser;
import com.boco.pciv.entity.sys.ResInfo;
import com.boco.pciv.entity.sys.ResTree;
import com.boco.pciv.service.sys.IResService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping(value="/sys/res/")
public class ResourceController extends PcivController {
    Logger logger = LoggerFactory.getLogger(ResourceController.class);
	@Resource
	private IResService svr;

    @GetMapping("/getResTreeByUser")
    public String getResTreeByUser() {
        LoginUser user = getCurUser();
        ResTree res = svr.getResTreeByUser(user.getUserId());
        return JSONUtil.toJsonStr(res);
    }

    @GetMapping("/list")
    public PageResultBase getResList(){
        List<ResInfo> list = svr.getAll();
        //addOperationLog("查询","资源信息查询");
        LayUIPageResult rtn = new LayUIPageResult();
        rtn.setCode(0);
        rtn.setData(list);
        rtn.setCount(new Long(list.size()));
        rtn.setPage(1);
        rtn.setLimit(list.size());
        return rtn;
    }

    @GetMapping("/resTree4Select")
    public BizResponse getResourceTree4Select(String parentId){
        List<SelectItem> items = svr.getResTreeByParent4Select(parentId);
        return  BizResponseUtil.list(items);
    }

    @GetMapping("/info")
    public BizResponse getResourceInfo(String id){
        ResInfo detail = svr.getOne(id);
        return  BizResponseUtil.single(detail);
    }

    @PostMapping("/add")
    public BizResponse addResource(ResInfo res){
        if(res ==null){
            return BizResponseUtil.fail("新增信息不能为空");
        }

        try{
            res.setCreateUser(getCurUserId());
            svr.add(res);
            addOperationLog("新增",String.format("新增资源:%s",res.getName()));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }

    @PostMapping("/delete")
    public BizResponse deleteResource(String id){
        try{
            svr.delete(id);
            addOperationLog("删除",String.format("删除资源:%s",id));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }

    @PostMapping("/edit")
    public BizResponse editResource(ResInfo res){
        if(res ==null){
            return BizResponseUtil.fail("信息不能为空");
        }

        try{
            res.setUpdateUser(getCurUserId());
            svr.edit(res);
            addOperationLog("编辑",String.format("编辑资源:%s",res.getName()));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }
}
